#include "toast.h"
#include "ui_toast.h"
#include <QTimer>

Toast::Toast(QString msg, QWidget *parent, bool isLong) :
    QDialog(parent),
    ui(new Ui::Toast),
    message(msg),
    mIsLong(isLong)
{
    ui->setupUi(this);

    // 属性设置
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setStyleSheet("background-color: rgba(0, 0, 0, 0.7); color: white; padding: 10px; border-radius: 5px;");

    ui->label->setText(msg);
    // label根据内容,其宽随之变化
    ui->label->adjustSize();

    // 仿toast 长短显示效果
    long time = 1500;
    if(isLong)
        time = 3000;
    QTimer::singleShot(time, this, &Toast::slotTimeOut);
}

Toast::~Toast()
{
    delete ui;
}

int Toast::getLabelWidth()
{
    return ui->label->width();
}

void Toast::slotTimeOut()
{
    this->close();
    deleteLater();
}
